﻿using ProjBackEnd.DBInstance;
using ProjBackEnd.Lib;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using ProjBackEnd.Areas.Sys.Models;

namespace ProjBackEnd.DAL
{
    public class basecodeLogic
    {
        public static bool CheckUserPermission(string _strMemberid, string _strControllorName)
        {
            return true; //****需添加后端权限设置相关的表结构
        }

        public static RespSysMenuInfo GetMenuFeatureAll()
        {
            RespSysMenuInfo respReturn = new RespSysMenuInfo();
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<MenuAll> lstMenuTarget = new List<MenuAll>();
                List<FeatureAll> lstFeatureTarget = new List<FeatureAll>();
                List<SYS_MENUTB> lstMenuSour = puctx.SYS_MENUTB.OrderBy(n => n.menuorder).ToList();
                foreach (SYS_MENUTB _entity in lstMenuSour)
                {
                    MenuAll collec = new MenuAll();
                    collec.createid = _entity.createid;
                    collec.formnm = _entity.formnm;
                    collec.menucd = _entity.menucd;
                    collec.menuchn = _entity.menunmchg;
                    collec.menueng = _entity.menunmeng;
                    collec.menuid = _entity.menuid;
                    collec.menuorder = _entity.menuorder;
                    collec.opentp = _entity.opentp;
                    collec.optlb = "X";
                    collec.pmenuid = _entity.pmenuid;
                    collec.iconurl = _entity.iconurl;
                    lstMenuTarget.Add(collec);
                }

                List<SYS_FEATURETB> lstFeatureSour = puctx.SYS_FEATURETB.ToList();
                foreach (SYS_FEATURETB _entity in lstFeatureSour)
                {
                    FeatureAll collec = new FeatureAll();
                    collec.featurecd = _entity.featurecd;
                    collec.featurenm = _entity.featurenm;
                    collec.isvalid = _entity.isvalid;
                    collec.menuid = _entity.menuid;
                    collec.optlb = "X";
                    lstFeatureTarget.Add(collec);
                }

                ResultMenu res = new ResultMenu();
                res.menulist = lstMenuTarget;
                res.featurelist = lstFeatureTarget;

                respReturn.result = res;
                respReturn.statusCode = 0;
                respReturn.statusMsg = "";
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 0;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
            return respReturn;
        }

        public static RespSysRolePermissonInfo GetPermissionbyRoleid(int _iRoleid)
        {
            RespSysRolePermissonInfo respReturn = new RespSysRolePermissonInfo();
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<RolePermission> lstRes = new List<RolePermission>();
                IEnumerable<VIEW_SYS_ROLEPERMISSION> lstSour = puctx.VIEW_SYS_ROLEPERMISSION.Where(n => n.roleid == _iRoleid);
                foreach (VIEW_SYS_ROLEPERMISSION _entity in lstSour)
                {
                    RolePermission collec = new RolePermission();
                    collec.featurecd = _entity.featurecd;
                    collec.featurenm = _entity.featurenm;
                    collec.menuid = _entity.menuid;
                    collec.menunmchn = _entity.menunmchg;
                    collec.roleid = _entity.roleid;
                    lstRes.Add(collec);
                }

                ResultRolePermissionInfo res = new ResultRolePermissionInfo();
                res.rolepermissionlist = lstRes;

                respReturn.result = res;
                respReturn.statusCode = 0;
                respReturn.statusMsg = "";
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 0;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
            return respReturn;
        }

        public static bool UpdateRolePermission(List<RolePermission> _lst)
        {
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            List<SYS_ROLEMENUFEATUREMAPPING> lstIns = new List<SYS_ROLEMENUFEATUREMAPPING>();
            List<SYS_ROLEMENUFEATUREMAPPING> lstDel = new List<SYS_ROLEMENUFEATUREMAPPING>();
            foreach (RolePermission _collec in _lst)
            {
                SYS_ROLEMENUFEATUREMAPPING entity = new SYS_ROLEMENUFEATUREMAPPING();
                entity.roleid = _collec.roleid;
                entity.menuid = _collec.menuid;
                entity.featurecd = _collec.featurecd;
                if (_collec.optlb == "I") lstIns.Add(entity);
                if (_collec.optlb == "D") lstDel.Add(entity);
            }
            if (lstIns.Count > 0) puctx.SYS_ROLEMENUFEATUREMAPPING.InsertAllOnSubmit(lstIns);
            if (lstDel.Count > 0)
            {
                puctx.SYS_ROLEMENUFEATUREMAPPING.AttachAll(lstDel);
                puctx.SYS_ROLEMENUFEATUREMAPPING.DeleteAllOnSubmit(lstDel);
            }
            try
            {
                puctx.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
                return true;
            }
            catch (Exception ex) { DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message); return false; }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
        }

        public static bool UpdateMenuInfo(string _strMemberid, MenuAll _entity)
        {
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            switch (_entity.optlb.ToUpper())
            {
                case "I":
                    int iMenuid = puctx.SP_GETMASTERKEY("MENUID");
                    if (iMenuid == -1) return false;
                    SYS_MENUTB entityNew = new SYS_MENUTB();
                    entityNew.createdt = DateTime.Now;
                    entityNew.createid = _strMemberid;
                    entityNew.formnm = _entity.formnm == null ? "" : _entity.formnm;
                    entityNew.menucd = _entity.menucd;
                    entityNew.menuid = iMenuid;
                    entityNew.menunmchg = _entity.menuchn ?? "";
                    entityNew.menunmeng = _entity.menueng ?? "";
                    entityNew.menuorder = _entity.menuorder;
                    entityNew.opentp = _entity.opentp;
                    entityNew.pmenuid = _entity.pmenuid;
                    entityNew.iconurl = _entity.iconurl ?? "";
                    puctx.SYS_MENUTB.InsertOnSubmit(entityNew);

                    SYS_FEATURETB collecNewFt = new SYS_FEATURETB();
                    collecNewFt.featurecd = "IN";
                    collecNewFt.featurenm = "进入";
                    collecNewFt.isvalid = "Y";
                    collecNewFt.menuid = iMenuid;
                    puctx.SYS_FEATURETB.InsertOnSubmit(collecNewFt);
                    break;
                case "U":
                    SYS_MENUTB entityUpd = puctx.SYS_MENUTB.SingleOrDefault(n => n.menuid == _entity.menuid);
                    if (entityUpd == default(SYS_MENUTB)) return false;
                    entityUpd.formnm = _entity.formnm;
                    entityUpd.menucd = _entity.menucd;
                    entityUpd.menunmchg = _entity.menuchn ?? "";
                    entityUpd.menunmeng = _entity.menueng ?? "";
                    entityUpd.menuorder = _entity.menuorder;
                    entityUpd.iconurl = _entity.iconurl ?? entityUpd.iconurl;
                    break;
                case "D":
                    SYS_MENUTB entityDel = puctx.SYS_MENUTB.SingleOrDefault(n => n.menuid == _entity.menuid);
                    if (entityDel == default(SYS_MENUTB)) return false;
                    puctx.SYS_MENUTB.DeleteOnSubmit(entityDel);

                    puctx.SYS_FEATURETB.DeleteAllOnSubmit(puctx.SYS_FEATURETB.Where(n => n.menuid == _entity.menuid));
                    puctx.SYS_ROLEMENUFEATUREMAPPING.DeleteAllOnSubmit(puctx.SYS_ROLEMENUFEATUREMAPPING.Where(n => n.menuid == _entity.menuid));
                    break;
                default:
                    break;
            }
            try
            {
                puctx.SubmitChanges();
                return true;
            }
            catch (Exception ex) { DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message); return false; }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
        }

        public static RespSysRoleInfo GetRoleInfo(string _strComcd)
        {
            RespSysRoleInfo respReturn = new RespSysRoleInfo();
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<SYS_ROLEGROUPTB> lstRolegroup = puctx.SYS_ROLEGROUPTB.OrderBy(n => n.grouporder).ToList();
                List<SYS_ROLETB> lstRole = puctx.SYS_ROLETB.Where(n=>n.comcd==_strComcd).ToList();
                List<RoleGroupAll> lstRoleGroupRet = new List<RoleGroupAll>();
                foreach (SYS_ROLEGROUPTB _entity in lstRolegroup)
                {
                    RoleGroupAll info = new RoleGroupAll();
                    info.roletp = _entity.roletp;
                    info.groupnm = _entity.groupnm;
                    info.grouporder = _entity.grouporder;
                    List<RoleInfoAll> lstRoleRet = new List<RoleInfoAll>();
                    foreach (SYS_ROLETB _entityRole in lstRole.Where(n => n.roletp == info.roletp))
                    {
                        RoleInfoAll infoRole = new RoleInfoAll();
                        infoRole.roleid = _entityRole.roleid;
                        infoRole.rolenm = _entityRole.rolenm;
                        infoRole.roletp = _entityRole.roletp;
                        infoRole.roletpnm = _entityRole.roletp;
                        infoRole.comcd = _entityRole.comcd;
                        lstRoleRet.Add(infoRole);
                    }
                    info.rolelist = lstRoleRet;
                    lstRoleGroupRet.Add(info);
                }
                ResultRoleInfo res = new ResultRoleInfo();
                res.rolegrouplist = lstRoleGroupRet;
                respReturn.result = res;
                respReturn.statusCode = 0;
                respReturn.statusMsg = "";
            }
            catch (Exception ex) { DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
            return respReturn;
        }

        public static RespSysRoleMemberInfo GetUserRoleMpList(int _iRoleid)
        {
            RespSysRoleMemberInfo respReturn = new RespSysRoleMemberInfo();
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<SYS_USERROLEMAPPING> lstSour = puctx.SYS_USERROLEMAPPING.Where(n => n.roleid == _iRoleid).ToList();
                List<RoleMember> lstRes = new List<RoleMember>();
                foreach (SYS_USERROLEMAPPING _entity in lstSour)
                {
                    RoleMember info = new RoleMember();
                    info.roleid = _entity.roleid;
                    info.memberid = _entity.memberid;
                    lstRes.Add(info);
                }

                ResultRoleMember res = new ResultRoleMember();
                res.rolememberlist = lstRes;

                respReturn.result = res;
                respReturn.statusCode = 0;
                respReturn.statusMsg = "";
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 0;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
            return respReturn;
        }

        public static bool UpdateRoleGroupInfo(string _strMemberid, RoleGroupforCUD _entityGroup, RoleforCUD _entityRole)
        {
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                if(_entityGroup!=default(RoleGroupforCUD))
                {
                    switch (_entityGroup.optlb) //角色分组
                    {
                        case "I":
                            SYS_ROLEGROUPTB entityNew = new SYS_ROLEGROUPTB();
                            entityNew.groupnm = _entityGroup.groupnm;
                            entityNew.grouporder = _entityGroup.grouporder;
                            entityNew.roletp = _entityGroup.roletp;
                            puctx.SYS_ROLEGROUPTB.InsertOnSubmit(entityNew);
                            break;
                        case "U":
                            SYS_ROLEGROUPTB entityUpd = puctx.SYS_ROLEGROUPTB.SingleOrDefault(n => n.roletp == _entityGroup.roletp);
                            if (entityUpd == default(SYS_ROLEGROUPTB)) return false;
                            entityUpd.roletp = _entityGroup.roletp;
                            entityUpd.groupnm = _entityGroup.groupnm;
                            entityUpd.grouporder = _entityGroup.grouporder;
                            break;
                        case "D":
                            if (puctx.SYS_ROLETB.Count(n => n.roletp == _entityGroup.roletp) > 0) return false;
                            puctx.SYS_ROLEGROUPTB.DeleteOnSubmit(puctx.SYS_ROLEGROUPTB.SingleOrDefault(n => n.roletp == _entityGroup.roletp));
                            break;
                        default:
                            return false;
                    }
                }

                if (_entityRole != default(RoleforCUD))
                {
                    switch (_entityRole.optlb) //角色
                    {
                        case "I":
                            int iRoleid = puctx.SP_GETMASTERKEY("ROLEID");
                            if (iRoleid == -1) return false;
                            SYS_ROLETB entityNew = new SYS_ROLETB();
                            entityNew.roleid = iRoleid;
                            entityNew.comcd = _entityRole.comcd;
                            entityNew.rolenm = _entityRole.rolenm;
                            entityNew.roletp = _entityRole.roletp;
                            entityNew.createdt = DateTime.Now;
                            entityNew.createid = _strMemberid;
                            puctx.SYS_ROLETB.InsertOnSubmit(entityNew);
                            break;
                        case "U":
                            SYS_ROLETB entityUpd = puctx.SYS_ROLETB.SingleOrDefault(n => n.roleid == _entityRole.roleid);
                            if (entityUpd == default(SYS_ROLETB)) return false;
                            entityUpd.rolenm = _entityRole.rolenm;
                            break;
                        case "D":
                            if (puctx.SYS_USERROLEMAPPING.Count(n => n.roleid == _entityRole.roleid) > 0) return false;
                            if (puctx.SYS_ROLEMENUFEATUREMAPPING.Count(n => n.roleid == _entityRole.roleid) > 0) return false;
                            puctx.SYS_ROLETB.DeleteOnSubmit(puctx.SYS_ROLETB.SingleOrDefault(n => n.roleid == _entityRole.roleid));
                            break;
                        default:
                            return false;
                    }
                }
                puctx.SubmitChanges();
                return true;
            }
            catch (Exception ex) { DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message); return false; }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
        }

        public static bool UpdateRoleMember(List<RoleMember> _lst)
        {
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            List<SYS_USERROLEMAPPING> lstIns = new List<SYS_USERROLEMAPPING>();
            List<SYS_USERROLEMAPPING> lstDel = new List<SYS_USERROLEMAPPING>();
            foreach (RoleMember _collec in _lst)
            {
                SYS_USERROLEMAPPING entity = new SYS_USERROLEMAPPING();
                entity.roleid = _collec.roleid;
                entity.memberid = _collec.memberid;
                if (_collec.optlb == "I") lstIns.Add(entity);
                if (_collec.optlb == "D") lstDel.Add(entity);
            }
            if (lstIns.Count > 0) puctx.SYS_USERROLEMAPPING.InsertAllOnSubmit(lstIns);
            if (lstDel.Count > 0)
            {
                puctx.SYS_USERROLEMAPPING.AttachAll(lstDel);
                puctx.SYS_USERROLEMAPPING.DeleteAllOnSubmit(lstDel);
            }
            try
            {
                puctx.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
                return true;
            }
            catch (Exception ex) { DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message); return false; }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
        }

        public static List<CD_BASIC> GetClassCodeList(string _strAttrcd)
        {
            List<CD_BASIC> lst = new List<CD_BASIC>();
            BaseCodeDataContext bcctx = new BaseCodeDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                lst = bcctx.CD_BASIC.Where(n => n.attrcd == _strAttrcd && n.scdlb != "1").ToList();
            }
            catch (Exception ex) { lst.Clear(); DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { if (bcctx.Connection.State != ConnectionState.Closed) bcctx.Connection.Close(); }
            return lst;
        }

        public static List<ComSummaryInfo> GetComSummary()
        {
            List<ComSummaryInfo> lst = new List<ComSummaryInfo>();
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<VIEW_CD_GPCOMINFO> lstEntity = puctx.VIEW_CD_GPCOMINFO.ToList();
                foreach (VIEW_CD_GPCOMINFO _entity in lstEntity)
                {
                    ComSummaryInfo info = new ComSummaryInfo();
                    info.comcd = _entity.comcd;
                    info.comnm = _entity.comnm;
                    info.gpcd = _entity.gpcd;
                    info.groupnm = _entity.gpnm;
                    lst.Add(info);
                }
            }
            catch (Exception ex) { lst.Clear(); DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
            return lst;
        }

        public static List<ComSummaryInfo> GetComSummary(string _strGpcd)
        {
            List<ComSummaryInfo> lst = new List<ComSummaryInfo>();
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<VIEW_CD_GPCOMINFO> lstEntity = puctx.VIEW_CD_GPCOMINFO.Where(n=>n.gpcd==_strGpcd).ToList();
                foreach (VIEW_CD_GPCOMINFO _entity in lstEntity)
                {
                    ComSummaryInfo info = new ComSummaryInfo();
                    info.comcd = _entity.comcd;
                    info.comnm = _entity.comnm;
                    info.gpcd = _entity.gpcd;
                    info.groupnm = _entity.gpnm;
                    lst.Add(info);
                }
            }
            catch (Exception ex) { lst.Clear(); DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
            return lst;
        }

        public static RespSysNullResultInfo UpdateComInfo(ComSummaryInfo _collec)
        {
            RespSysNullResultInfo respReturn = new RespSysNullResultInfo();
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                switch (_collec.optlb.ToUpper())
                {
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        puctx.CD_COMTB.InsertOnSubmit(new CD_COMTB { comcd = _collec.comcd, comnm = _collec.comnm, gpcd = _collec.gpcd, taxno = "" });
                        break;
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        CD_COMTB entityUpd = puctx.CD_COMTB.SingleOrDefault(n => n.comcd == _collec.comcd);
                        if (entityUpd == default(CD_COMTB)) throw new BizException("未找到公司信息。");

                        entityUpd.comnm = _collec.comnm ?? entityUpd.comnm;
                        entityUpd.gpcd = _collec.gpcd ?? entityUpd.gpcd;
                        break;
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        throw new BizException("暂不支持公司信息的删除操作。");
                    default: throw new Exception("操作标识符异常");
                }
                puctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
            return respReturn;
        }


        public static RespSysGroupInfo GetGroupList()
        {
            RespSysGroupInfo respReturn = new RespSysGroupInfo();
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<GroupInfo> lst = puctx.CD_GROUPTB.AsEnumerable().Select(n => new GroupInfo { gpcd = n.gpcd, gpnm = n.gpcd }).ToList();
                respReturn.result = new ResultGroupInfo { gplist = lst };
            }
            catch (Exception ex)
            {
                respReturn.statusCode = 90005;
                respReturn.statusMsg = ex.Message;
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
            }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
            return respReturn;
        }

        public static RespSysNullResultInfo UpdateGroupInfo(GroupInfo _collec)
        {
            RespSysNullResultInfo respReturn = new RespSysNullResultInfo();
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                switch (_collec.optlb.ToUpper())
                {
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        puctx.CD_GROUPTB.InsertOnSubmit(new CD_GROUPTB { gpcd = _collec.gpcd, gpnm = _collec.gpnm });
                        break;
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        CD_GROUPTB entityUpd = puctx.CD_GROUPTB.SingleOrDefault(n => n.gpcd == _collec.gpcd);
                        if (entityUpd == default(CD_GROUPTB)) throw new BizException("未找到集团信息。");
                        if (entityUpd.gpcd != _collec.gpcd && puctx.CD_COMTB.Count(n => n.gpcd == _collec.gpcd) > 0) throw new BizException("有子记录，无法修改。");
                        entityUpd.gpcd = _collec.gpcd ?? entityUpd.gpcd;
                        entityUpd.gpnm = _collec.gpnm ?? entityUpd.gpnm;
                        break;
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        if (puctx.CD_COMTB.Count(n => n.gpcd == _collec.gpcd) > 0) throw new BizException("有子记录，无法删除。");
                        puctx.CD_GROUPTB.DeleteOnSubmit(puctx.CD_GROUPTB.SingleOrDefault(n => n.gpcd == _collec.gpcd));
                        break;
                    default: throw new Exception("操作标识符异常");
                }
                puctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
            return respReturn;
        }

        public static RespSysDeptInfo GetDeptList(string _strComcd)
        {
            RespSysDeptInfo respReturn = new RespSysDeptInfo();
            List<DeptInfo> lst = new List<DeptInfo>();
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                List<CD_DEPTTB> lstEntity = puctx.CD_DEPTTB.Where(n => n.comcd == _strComcd).ToList();
                foreach (CD_DEPTTB _entity in lstEntity)
                {
                    lst.Add(new DeptInfo
                    {
                        comcd = _strComcd,
                        deptcd = _entity.deptcd,
                        deptnm = _entity.deptnm,
                        deptstat = _entity.deptnm,
                        optlb = GlobalSettings.ENTITYOPERATION_NONE
                    });
                }
                respReturn.result = new ResultDeptSummary { deptsummarylist = lst };
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
            return respReturn;
        }

        public static RespSysNullResultInfo UpdateDept(DeptInfo _collec)
        {
            RespSysNullResultInfo respReturn = new RespSysNullResultInfo();
            ProjUserInfoDataContext puctx = new ProjUserInfoDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                switch (_collec.optlb.ToUpper())
                {
                    case GlobalSettings.ENTITYOPERATION_INSERT:
                        int iComCount = puctx.CD_COMTB.Count(n => n.comcd == _collec.comcd);
                        if (iComCount != 1) throw new BizException("公司代码异常"); //新增部门前必须新增所属公司
                        int iDeptcd = puctx.SP_GETMASTERKEY("DEPTCD");
                        if (iDeptcd == -1) throw new Exception("获取流水号失败");
                        CD_DEPTTB entityIns = new CD_DEPTTB();
                        entityIns.comcd = _collec.comcd;
                        entityIns.deptcd = _collec.comcd + iDeptcd.ToString();
                        entityIns.deptnm = _collec.deptnm;
                        entityIns.stat = "1"; //****保留字段
                        puctx.CD_DEPTTB.InsertOnSubmit(entityIns);
                        break;
                    case GlobalSettings.ENTITYOPERATION_UPDATE:
                        CD_DEPTTB entityUpd = puctx.CD_DEPTTB.SingleOrDefault(n => n.deptcd == _collec.deptcd);
                        if (entityUpd == default(CD_DEPTTB)) throw new BizException("未找到部门信息");
                        if (entityUpd.comcd != _collec.comcd) throw new BizException("所属公司与记录不符");
                        entityUpd.deptnm = string.IsNullOrEmpty(_collec.deptnm) ? entityUpd.deptnm : _collec.deptnm;
                        //entityUpd.stat = string.IsNullOrEmpty(_collec.deptstat) ? entityUpd.stat : _collec.deptstat; //****保留字段
                        break;
                    case GlobalSettings.ENTITYOPERATION_DELETE:
                        CD_DEPTTB entityDel = puctx.CD_DEPTTB.SingleOrDefault(n => n.deptcd == _collec.deptcd);
                        if (entityDel == default(CD_DEPTTB)) throw new BizException("未找到部门信息");
                        int iDeptUserCount = puctx.CD_DEPTUSERMAPPING.Count(n => n.deptcd == _collec.deptcd);
                        if (iDeptUserCount > 0) throw new BizException("该部门下有隶属成员，无法变更");
                        puctx.CD_DEPTTB.DeleteOnSubmit(entityDel);
                        break;
                    default: throw new Exception("操作标识符异常");
                }
                puctx.SubmitChanges();
                respReturn.statusMsg = "操作成功";
            }
            catch (BizException ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90021;
                respReturn.statusMsg = ex.Message;
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (puctx.Connection.State != ConnectionState.Closed) puctx.Connection.Close(); }
            return respReturn;
        }

        /// <summary>
        /// 获取指定分类的动态代码
        /// </summary>
        /// <param name="_strAttrcd">代码分类。如果为空就获取用户列表和所有所需的动态代码，否则仅获取指定分类的代码</param>
        /// 如未来环境所需的初始代码类别增加，则需要在ResultDynamicCode类中增加相应分类的列表
        /// <returns></returns>
        public static RespSysDynamicCode GetDynamicCode(string _strAttrcd)
        {
            RespSysDynamicCode respReturn = new RespSysDynamicCode();
            ResultDynamicCode res = new ResultDynamicCode();
            List<DynamicCodeInfo> lstCommoncdInfo = new List<DynamicCodeInfo>();
            BaseCodeDataContext bcctx = new BaseCodeDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                IEnumerable<CD_BASIC> lstCode = bcctx.CD_BASIC.Where(n => n.scdlb != "1" && n.attrcd == _strAttrcd).OrderBy(n=>n.sortval);
                foreach (CD_BASIC _entityCode in lstCode)
                {
                    DynamicCodeInfo info = new DynamicCodeInfo();
                    info.attrcd = _strAttrcd;
                    info.cd = _entityCode.cd;
                    info.descchn = _entityCode.speccn;
                    info.desceng = _entityCode.specen;
                    info.othcd = _entityCode.othcd; //辅助代码
                    info.sortval = _entityCode.sortval;
                    lstCommoncdInfo.Add(info);
                }
                res.classcodelist = lstCommoncdInfo;
                respReturn.result = res;
                respReturn.statusCode = 0;
                respReturn.statusMsg = "获取成功";
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bcctx.Connection.State != ConnectionState.Closed) bcctx.Connection.Close(); }
            return respReturn;
        }

        public static RespSysNullResultInfo UpdateDynamicCode(string _strMemberid, DynamicCodeInfo _collec)
        {
            RespSysNullResultInfo respReturn = new RespSysNullResultInfo();
            BaseCodeDataContext bcctx = new BaseCodeDataContext(sysLogic.GetConnString(sysLogic.DataBaseName.PROJ));
            try
            {
                switch (_collec.optlb.ToUpper())
                {
                    case "I":
                        CD_BASIC entityIns = new CD_BASIC();
                        entityIns.attrcd = _collec.attrcd;
                        entityIns.cd = _collec.cd;
                        entityIns.speccn = string.IsNullOrEmpty(_collec.descchn) ? "" : _collec.descchn;
                        entityIns.specen = string.IsNullOrEmpty(_collec.desceng) ? "" : _collec.desceng;
                        entityIns.othcd = _collec.othcd ?? "";
                        entityIns.scdlb = entityIns.freezelb = "0";
                        entityIns.sortval = _collec.sortval;
                        entityIns.createid = _strMemberid;
                        entityIns.createdt = entityIns.moddt = DateTime.Now;
                        entityIns.remark = "";
                        bcctx.CD_BASIC.InsertOnSubmit(entityIns);
                        break;
                    case "U":
                        CD_BASIC entityUpd = bcctx.CD_BASIC.SingleOrDefault(n => n.attrcd == _collec.attrcd && n.cd == _collec.cd);
                        if (entityUpd == default(CD_BASIC))
                        {
                            respReturn.statusCode = 60036;
                            respReturn.statusMsg = "未找到需要操作的基础代码";
                            return respReturn;
                        }
                        IEnumerable<CD_BASIC> ieChildUpd = bcctx.CD_BASIC.Where(n => n.attrcd == _collec.cd);
                        if (ieChildUpd.Count() > 0 && entityUpd.cd != _collec.cd)
                        {
                            foreach (CD_BASIC _entity in ieChildUpd) _entity.cd = _collec.cd;
                        }
                        entityUpd.cd = _collec.cd;
                        entityUpd.speccn = _collec.descchn ?? entityUpd.speccn;
                        entityUpd.specen = _collec.desceng ?? entityUpd.specen;
                        entityUpd.sortval = _collec.sortval;
                        entityUpd.othcd = _collec.othcd ?? entityUpd.othcd;
                        entityUpd.moddt = DateTime.Now;
                        break;
                    case "D":
                        CD_BASIC entityDel = bcctx.CD_BASIC.SingleOrDefault(n => n.attrcd == _collec.attrcd && n.cd == _collec.cd);
                        if (entityDel == default(CD_BASIC))
                        {
                            respReturn.statusCode = 60036;
                            respReturn.statusMsg = "未找到需要操作的基础代码";
                            return respReturn;
                        }
                        IEnumerable<CD_BASIC> ieChildDel = bcctx.CD_BASIC.Where(n => n.attrcd == _collec.cd);
                        if (ieChildDel.Count() > 0) throw new Exception("有子记录不能删除");
                        bcctx.CD_BASIC.DeleteOnSubmit(entityDel);
                        break;
                    default: break;
                }
                bcctx.SubmitChanges();
                respReturn.statusCode = 0;
                respReturn.statusMsg = "更新成功";
            }
            catch (Exception ex)
            {
                DataUtil.WriteLog(MethodBase.GetCurrentMethod().Name, ex.Message);
                respReturn.statusCode = 90005;
                respReturn.statusMsg = "数据访问异常。" + ex.Message;
            }
            finally { if (bcctx.Connection.State != ConnectionState.Closed) bcctx.Connection.Close(); }
            return respReturn;
        }

    }
}